﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoastalClimbing.Code;
using System.Web.Security;
using System.Data;

namespace CoastalClimbing.Auth
{
    public partial class Routes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    string action = Request.QueryString["action"].ToString();
                    string message = string.Empty;

                    switch (action)
                    {
                        case "d":
                            message = "Record successfully deleted.";
                            break;
                        case "a":
                            message = "Record successfully added.";
                            break;
                        case "e":
                            message = "Record successfully edited.";
                            break;
                        default:
                            break;
                    }

                    lblMessage.Text = message;
                }
                catch { }

                this.BindInfo();
            }
        }

        private void BindInfo()
        {
            try
            {
                grvRoutes.DataSource = DAL.RoutesSelectAll();
                grvRoutes.DataBind();
                
                BindDDL(ddlRating, DAL.RatingsSelectAll(), "RATINGSTATUSTRANSLATION", "ID", string.Empty);
                BindDDL(ddlSetter, DAL.SettersSelectAll(), "SETTER", "ID", string.Empty);
                BindDDL(ddlWall, DAL.WallsGet(), "NAME", "ID", string.Empty);
                txtDateExp.Text = GetViewState<string>("DATEEXP");
                txtDateSet.Text = GetViewState<string>("DATESET");
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }
        }

        private T GetViewState<T>(string viewStateName)
        {
            T t = default(T);

            if(ViewState[viewStateName] != null)
            {
                try
                {
                    t = (T)ViewState[viewStateName];
                }
                catch { }
            }

            return t;
        }

        private void BindDDL(DropDownList ddl, DataTable dt, string dataTextField, string dataValueField, string setValue)
        {
            ddl.DataSource = dt;
            ddl.DataTextField = dataTextField;
            ddl.DataValueField = dataValueField;

            if (!string.IsNullOrEmpty(setValue))
                ddl.SelectedValue = setValue;

            ddl.DataBind();
        }

        protected void CancelRecord(object sender, GridViewCancelEditEventArgs e)
        {
            grvRoutes.EditIndex = -1;
            this.BindInfo();
        }

        protected void EditRecord(object sender, GridViewEditEventArgs e)
        {
            grvRoutes.EditIndex = e.NewEditIndex;
            this.BindInfo();

            DropDownList ddlrRating = (DropDownList)grvRoutes.Rows[e.NewEditIndex].FindControl("ddlRating");
            Label lblRatingID = (Label)grvRoutes.Rows[e.NewEditIndex].FindControl("lblRatingID");
            DropDownList ddlrSetter = (DropDownList)grvRoutes.Rows[e.NewEditIndex].FindControl("ddlSetter");
            Label lblSetterID = (Label)grvRoutes.Rows[e.NewEditIndex].FindControl("lblSetterID");
            DropDownList ddlrWall = (DropDownList)grvRoutes.Rows[e.NewEditIndex].FindControl("ddlWall");
            Label lblWallID = (Label)grvRoutes.Rows[e.NewEditIndex].FindControl("lblWallID");

            BindDDL(ddlrRating, DAL.RatingsSelectAll(), "RATINGSTATUSTRANSLATION", "ID", lblRatingID.Text);
            BindDDL(ddlrSetter, DAL.SettersSelectAll(), "SETTER", "ID", lblSetterID.Text);
            BindDDL(ddlrWall, DAL.WallsGet(), "NAME", "ID", lblWallID.Text);
        }

        protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
        {
            string id = grvRoutes.DataKeys[e.RowIndex].Value.ToString();

            GridViewRow row = grvRoutes.Rows[e.RowIndex];
            TextBox txtgName = (TextBox)row.FindControl("txtName");
            TextBox txtgDescription = (TextBox)row.FindControl("txtDescription");
            DropDownList ddlgSetter = (DropDownList)row.FindControl("ddlSetter");
            DropDownList ddlgWall = (DropDownList)row.FindControl("ddlWall");
            DropDownList ddlgRating = (DropDownList)row.FindControl("ddlRating");
            TextBox txtgDateSet = (TextBox)row.FindControl("txtDateSet");
            TextBox txtgDateExp = (TextBox)row.FindControl("txtDateExp");

            DateTime dateSet = Convert.ToDateTime("01-01-1900 00:00:00.000");
            DateTime dateExp = Convert.ToDateTime("01-01-1900 00:00:00.000");

            if(!string.IsNullOrWhiteSpace(txtgDateSet.Text))
            {
                dateSet = Convert.ToDateTime(txtgDateSet.Text);
            }
            if (!string.IsNullOrWhiteSpace(txtgDateExp.Text))
            {
                dateExp = Convert.ToDateTime(txtgDateExp.Text);
            }

            if (!string.IsNullOrWhiteSpace(txtgName.Text))
            {
                DAL.RoutesEdit(id, txtgName.Text, txtgDescription.Text, ddlgSetter.SelectedValue, ddlgRating.SelectedValue, ddlgWall.SelectedValue, Membership.GetUser().ProviderUserKey.ToString(), dateSet, dateExp);
            }

            grvRoutes.EditIndex = -1;

            Response.Redirect("~/Auth/Routes.aspx?action=e");
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtName.Text))
            {
                ViewState["DATESET"] = txtDateSet.Text;
                ViewState["DATEEXP"] = txtDateExp.Text;
                DAL.RoutesCreate(txtName.Text, txtDescription.Text, ddlSetter.SelectedValue, ddlRating.SelectedValue, ddlWall.SelectedValue, Membership.GetUser().ProviderUserKey.ToString(), txtDateSet.Text, txtDateExp.Text);
                Response.Redirect("~/Auth/Routes.aspx?action=a");
            }
        }

        protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
        {
            string id = grvRoutes.DataKeys[e.RowIndex].Value.ToString();
            DAL.RoutesDelete(id);
            Response.Redirect("~/Auth/Routes.aspx?action=d");
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
                l.Attributes.Add("onclick", "javascript:return confirm('You are about to delete this route?')");
            }
        }

        protected void btnDeleteAllRoutes_Click(object sender, EventArgs e)
        {
            string message = txtDeleteAllRoutes.Text;

            if(message == "shadow")
            {
                DAL.RoutesDeleteAll();
                Response.Redirect("~/Auth/Routes.aspx?action=d");
            }
        }        
    }
}